# -*- coding: utf-8 -*-

from app import app, db
from config_user import ROLE_ADMIN,ROLE_TEACHER,DISTRICT_XIANLIN,STU_INIT,INVALID_TAG 

from .utilities import NjuUtility

import datetime

#咨询师表结构
class Teacher(db.Model):
    __tablename__ = 'tch'
    id = db.Column(db.Integer, primary_key = True)
    seq = db.Column(db.Integer, index = True)
    name = db.Column(db.String(100),index=True)
    contact = db.Column(db.String(16),index=True)
    email = db.Column(db.String(128), index=True)
    password = db.Column(db.String(60))
    href = db.Column(db.String(100))
    position = db.Column(db.SmallInteger)
    intro = db.Column(db.UnicodeText)
    imgname = db.Column(db.String(260))
    imgfile = db.Column(db.LargeBinary)    
    
    role = db.Column(db.SmallInteger)
    create_time = db.Column(db.DateTime)

    def __init__(self,seq,name,contact,email,password,href,pos=DISTRICT_XIANLIN,intro=u'',imgf=None,role=ROLE_TEACHER,create_time=datetime.datetime.now()):
        self.seq = seq
        self.name = name
        self.contact = contact
        self.email = email
        self.password = NjuUtility.md5Encode(password)
        self.href = href
        self.position = pos
        self.intro = intro
        self.imgname = "img_"+str(int(seq))+"_"+contact
        self.imgfile = imgf
        self.role = role
        self.create_time = create_time
        
    def __repr__(self):
        return '<Teacher %r>' % (self.name)

    def set_password(self, pw):
        self.password = NjuUtility.md5Encode(pw)
        
    def verify_password(self,pw):
        return pw==self.password or NjuUtility.md5Encode(pw)==self.password
    
    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        try:
            return unicode(self.id)
        except NameError:
            return str(self.id)
        
    def is_admin(self):
        return self.role == ROLE_ADMIN
        
#咨询师排班表
class Schedule(db.Model):
    __tablename__ = 'sch'
    id = db.Column(db.Integer, primary_key = True)
    tch_id = db.Column(db.Integer, index=True)  # 代表咨询师序号seq而非数据库自动递增的id
    tch_name = db.Column(db.String(100))
    tch_contact = db.Column(db.String(16))
    wkd_id = db.Column(db.String(64))
    stm_id = db.Column(db.String(64))
    campus = db.Column(db.SmallInteger)
    isboot = db.Column(db.SmallInteger, default = 0)
    currecid = db.Column(db.Integer)
    create_time = db.Column(db.DateTime)
    
    def __init__(self,tcid,tcna,tccont,wkd,stm,cam,create_time=datetime.datetime.now()):
        self.tch_id = tcid
        self.tch_name = tcna
        self.tch_contact = tccont
        self.wkd_id = wkd
        self.stm_id = stm
        self.campus = cam
        self.isboot = 0
        self.currecid = INVALID_TAG
        self.create_time = create_time
    
    def __repr__(self):
        return '<Schedule %r>' % (str(self.id))

#学生用户表结构
class Student(db.Model):
    __tablename__ = 'stu'
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(120),index = True)
    sex = db.Column(db.String(64))
    contact = db.Column(db.String(20), index = True, unique = True)
    password = db.Column(db.String(60))
    stuID = db.Column(db.String(64), index = True, unique = True)
    email = db.Column(db.String(128))
    department = db.Column(db.String(120))
    major = db.Column(db.String(120))
    grade = db.Column(db.String(120))
    canceltimes = db.Column(db.SmallInteger)
    status = db.Column(db.SmallInteger)
    currecid = db.Column(db.Integer)
    create_time = db.Column(db.DateTime)

    def __init__(self,name,sex,contact,password,stuid,email,department,major,grade,can_times=0,status=STU_INIT, create_time=datetime.datetime.now()):
        self.name = name
        self.sex = sex
        self.contact = contact
        self.password = NjuUtility.md5Encode(password)
        self.stuID = stuid
        self.email = email
        self.department = department
        self.major = major
        self.grade = grade
        self.canceltimes = can_times
        self.status = status
        self.currecid = INVALID_TAG
        self.create_time = create_time
        
    def __repr__(self):
        return '<User %r>' % (self.name)

    def verify_password(self,pw):
        #print(pw,self.password)
        return pw==self.password or NjuUtility.md5Encode(pw)==self.password
        
    def set_password(self, pw):
        self.password = NjuUtility.md5Encode(pw)
        
    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        try:
            return unicode(self.id)
        except NameError:
            return str(self.id)

# 预约历史纪录
class HistoryRecord(db.Model):
    __tablename__ = 'his_rec'
    id = db.Column(db.Integer, primary_key = True)
    tch_id = db.Column(db.Integer)
    tch_name = db.Column(db.String(100))
    stu_id = db.Column(db.Integer, index=True)
    stu_name = db.Column(db.String(100))
    pb_id = db.Column(db.Integer, index=True)
    date = db.Column(db.String(128))
    status = db.Column(db.SmallInteger)
    content = db.Column(db.String(512))
    demands = db.Column(db.UnicodeText)
    feedback = db.Column(db.UnicodeText)
    s_can_res = db.Column(db.UnicodeText)
    t_can_res = db.Column(db.UnicodeText)
    abstract = db.Column(db.UnicodeText)
    timestamp = db.Column(db.DateTime)
    
    def __init__(self,tc_id,tc_name,st_id,st_name,pbid,date,status,cont,demands=u"",curtime=datetime.datetime.now()):
        self.tch_id = tc_id
        self.tch_name = tc_name
        self.stu_id = st_id
        self.stu_name = st_name
        self.pb_id = pbid
        self.date = date
        self.status = status
        self.content = cont
        self.demands = demands
        self.s_can_res = u""
        self.t_can_res = u""
        self.timestamp = curtime
    
    def __repr__(self):
        return '<HistoryRecord %d>' % self.id
        
#咨询内容
class SubsContent(db.Model):
    __tablename__ = 'subs_cont'
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(128),unique = True)
    create_time = db.Column(db.DateTime)
    
    def __init__(self, cont, create_time=datetime.datetime.now()):
        self.content = cont
        self.create_time = create_time
    
#咨询时间
class SubsTime(db.Model):
    __tablename__ = 'subs_time'
    id = db.Column(db.Integer, primary_key=True)
    stm = db.Column(db.String(128),unique = True)
    create_time = db.Column(db.DateTime)
    
    def __init__(self,stm,create_time=datetime.datetime.now()):
        self.stm = stm
        self.create_time = create_time


class Article(db.Model):
    __tablename__ = 'article'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200))
    description = db.Column(db.String(500))
    img = db.Column(db.String(200))
    date = db.Column(db.String(30))
    content = db.Column(db.UnicodeText)
    type = db.Column(db.Integer)
    link = db.Column(db.String(500))
    create_time = db.Column(db.DateTime)

    def __init__(self, title, description, img, date, content, type, link, create_time=datetime.datetime.now()):
        self.title = title
        self.description = description
        self.img = img
        self.content = content
        self.type = type
        self.date = date
        self.link = link
        self.create_time = create_time

    def __repr__(self):
        return '<Article %r>' % (self.id)

    def get_id(self):
        try:
            return unicode(self.id)
        except NameError:
            return str(self.id)

class Slider(db.Model):
    __tablename__ = 'slider'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200))
    img = db.Column(db.String(200))
    link = db.Column(db.String(500))
    create_time = db.Column(db.DateTime)

    def __init__(self, title, img, link, create_time=datetime.datetime.now()):
        self.title = title
        self.img = img
        self.link = link
        self.create_time = create_time

    def __repr__(self):
        return '<Slider %r>' % (self.id)

    def get_id(self):
        try:
            return unicode(self.id)
        except NameError:
            return str(self.id)
            
            
class Notes(db.Model):
    __tablename__ = 'notes'
    id = db.Column(db.Integer, primary_key=True)
    seq = db.Column(db.Integer, index=True)
    content = db.Column(db.UnicodeText)
    type = db.Column(db.String(128))
    create_time = db.Column(db.DateTime)
    
    def __init__(self, seq, content, type, create_time=datetime.datetime.now()):
        self.seq = seq
        self.content = content
        self.type = type
        self.create_time = create_time
        
    def __repr__(self):
        return '<Notes %r>' % (self.id)
        
    def get_id(self):
        try:
            return unicode(self.id)
        except NameError:
            return str(self.id)
    
    
